Skip to content

Validates package root URIs - #10096

Open
johnpryan wants to merge 8 commits into
masterfrom
harden-dtd-workspace-roots
Open

johnpryan wants to merge 8 commits into
masterfrom
harden-dtd-workspace-roots

Conversation

@johnpryan

@johnpryan johnpryan commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

This change validates and resolves symlinks for the detected directory in VmServiceHandler before caching the resolved URI or updating DTD, requiring that it:

  • Is a local file:// URI with no authority, query, fragment, or null bytes that converts cleanly to a local file path
  • Does not point or resolve (via symlinks) to the filesystem root, the user's home directory (HOME / USERPROFILE), or an ancestor of it, and exists on disk
  • Contains either pubspec.yaml or .dart_tool/package_config.json.

It also extracts shared isValidLocalFileUri, isValidLocalFileUriString, and invalidLocalFileUriMessage helpers in file_utils.dart (reused by ExtensionsManager and the extensions API handler) and adds unit and server API tests covering valid and symlink-resolved roots, invalid URI forms, filesystem/home/home-ancestor root rejection, missing package markers, and end-to-end /api/notifyForVmServiceConnection handling.

Makes progress towards:

This change validates the detected directory in VmServiceHandler before caching
it or updating DTD, requiring that it:

is a local file:// URI with no authority, query, fragment, or dot-segment
traversal (. / ..), does not point or resolve (via symlinks) to the filesystem
root or the user's home directory (HOME / USERPROFILE), and exists on disk and
contains either pubspec.yaml or .dart_tool/package_config.json.

It also extracts shared isValidLocalFileUri and isValidLocalFileUriString
helpers in file_utils.dart (reused by ExtensionsManager and the extensions API
handler) and adds unit and server API tests covering valid roots, invalid URI
forms, filesystem/home root rejection, missing package markers, and end-to-end
/api/notifyForVmServiceConnection handling.

Makes progress towards:
* flutter/dash-devexp-internal#88
* flutter/dash-devexp-internal#93

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces package root validation in VmServiceHandler before caching or updating Dart Tooling Daemon (DTD) workspace roots, utilizing new local file URI validation helpers to prevent path traversal and reject invalid roots (such as the filesystem root or user home directory). The review feedback highlights a critical logical issue where validating the package root during disconnect flows can prevent proper cleanup of DTD workspace roots if the directory or its package markers were deleted during the session. It is recommended to skip validation when connected is false to ensure stale roots are successfully removed.

@johnpryan
johnpryan requested a review from a team as a code owner November 18, 2026 23:30
@johnpryan
johnpryan requested review from srawlins and removed request for a team November 18, 2026 23:30
Comment thread packages/devtools_shared/CHANGELOG.md Resolved
… validation.

* Reject encoded slashes (`/` / `%2f`) in `isValidLocalFileUri` path segments and validate raw `root` strings with `isValidLocalFileUriString` before `Uri.parse` in `detectRootPackageForVmService`, with new `%2f` test coverage.
* Simplify `_validateRootPackageUri` to check `dir.existsSync()` first, resolve symlinks once (failing closed on `FileSystemException`), and compare only the resolved path against the filesystem root and resolved home directory.
* Remove duplicate `_validateRootPackageUri` validation from `updateDtdWorkspaceRoots` and move its rejection tests to standalone `isValidRootPackageUri` unit tests without DTD.
* Extract the repeated `runWithRetry` polling blocks in `detectRootPackageForVmService` tests into a shared `detectRootWithRetry` helper.
* Update invalid-URI error messages in `_vm_service.dart` and `_devtools_extensions.dart`, add doc comments to `VmServiceHandler` constants, remove redundant `@visibleForTesting`, and document the new helpers and stricter URI checks in `packages/devtools_shared/CHANGELOG.md`.
- Move `_invalidLocalFileUriMessage` to a library-private constant in `server_api.dart` and reuse it in `_devtools_extensions.dart` and `_vm_service.dart` so invalid local file URI error messages stay in sync.
- Expose `VmServiceHandler.validateRootPackageUri` as `@visibleForTesting`, remove `isValidRootPackageUri`, and assert on specific rejection reason messages in `general_api_test.dart`.
- Trim the invalid local file URI test in `general_api_test.dart` to a single query URI case since `file_utils_test.dart` already covers all invalid URI components.
- Restore plain `Uri.parse` fixtures in `updateDtdWorkspaceRoots` tests and remove the unnecessary temporary directory setup and deleted-directory test.
- Document `detectRootWithRetry`'s default `isReady` behavior and simplify its `isReady` callbacks to use null-aware `contains`.
…e-roots

# Conflicts:
#	packages/devtools_shared/CHANGELOG.md
#	packages/devtools_shared/lib/src/server/handlers/_devtools_extensions.dart
#	packages/devtools_shared/lib/src/server/handlers/_vm_service.dart
#	packages/devtools_shared/test/server/general_api_test.dart

@bkonyi bkonyi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments on URI validation and symlink handling.

(Note: This review was posted by an AI agent on behalf of @bkonyi.)

@johnpryan

Copy link
Copy Markdown
Contributor Author

@kenzieschmoll @bkonyi this is ready for another look

bool isValidLocalFileUri(Uri uri) {
if (uri.scheme != 'file' ||
uri.authority.isNotEmpty ||
!uri.hasAbsolutePath ||

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still lets Windows UNC paths slip through via four leading slashes (e.g. file:////server/share/my_app). That parses with an empty authority and path == '//server/share/my_app', which uri.toFilePath() turns into \\server\share\my_app on Windows, reaching Directory.existsSync() (and even registering file://server/share/my_app with DTD if pubspec.yaml exists on the share).

Can we also reject uri.path.startsWith('//') here and add a test case for file:////server/share/my_app in file_utils_test.dart?

);
}

return (resolvedUri: Uri.file(resolvedDirPath), error: null);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Windows, resolveSymbolicLinksSync() can resolve a symlink to a UNC share (or a mapped network drive) into \\server\share\..., so Uri.file(resolvedDirPath) ends up as file://server/share/.... That violates isValidLocalFileUri and will cause ExtensionsManager._assertUriFormat to throw an ArgumentError later. Can we construct resolvedUri = Uri.file(resolvedDirPath) right after resolving symlinks and check isValidLocalFileUri(resolvedUri) before proceeding?

}

/// Error description for invalid local `file://` URIs.
const invalidLocalFileUriMessage =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: do we want invalidLocalFileUriMessage (a lowercase error-message fragment) exported as public API from devtools_shared.dart? Since all callers live inside devtools_shared, I'd lean toward hide-ing it in devtools_shared.dart (and dropping it from the CHANGELOG.md entry).


/// Resolves symbolic links for [directory] if it exists, returning `null` if
/// it does not exist or resolution fails.
static String? _tryResolveSymbolicLinks(Directory directory) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: resolveSymbolicLinksSync() already throws FileSystemException when the directory doesn't exist, so existsSync() here is redundant. Since this only has one caller, I'd probably inline the try/on FileSystemException in _isUserHomeOrAncestorDirectory.

uri: null,
);
}
final validationResult = validateRootPackageUri(parsedRoot);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we can destructure the record and check resolvedUri == null to avoid the !:

final (:resolvedUri, :error) = validateRootPackageUri(parsedRoot);
if (resolvedUri == null) {
  return (success: false, message: error, uri: null);
}
rootPackageUri = resolvedUri;

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants